#!/bin/bash

# Automated regression tester for SLDT components
#
# Author:   Mike Murphy <mamurph@cs.clemson.edu>
# Revision: 7 January 2009
#
# Copyright 2008-2009 Clemson University
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# This material is based upon work supported under a National Science
# Foundation Graduate Research Fellowship. Any opinions, findings,
# conclusions or recommendations expressed in this publication are those of
# the author(s) and do not necessarily reflect the views of the National
# Science Foundation.


# Need to exit with a code that Make can understand
GLOBAL_CODE=0

show_one() {
   echo "================ Begin Test: $n ================"
   echo "------------------- INPUT ----------------------"
   cat "$1"
   echo "------------------- OUTPUT ---------------------"
   python $COMPONENT "$1" $CLASSNAME
   code=$?
   if [ -n "`echo $n | grep ^P_`" ]; then
      if [ $code -ne 0 ]; then
         GLOBAL_CODE=$code
      fi
   elif [ -n "`echo $n | grep ^N_`" ]; then
      if [ $code -eq 0 ]; then
         GLOBAL_CODE=99
      fi
   fi
   echo "================ End Test: $n =================="
   echo ""
}

show_all() {
   for n in `ls "$TESTDIR"`; do
      show_one "$TESTDIR/$n"
   done
}

show_broken() {
   for n in `ls "$TESTDIR"`; do
      if [ -n "`echo $n | grep ^P_`" -o -n "`echo $n | grep ^N_`" ]; then
         result=`python $COMPONENT "$TESTDIR/$n" $CLASSNAME 2>&1`
         code=$?
      fi
      if [ -n "`echo $n | grep ^P_`" ]; then
         if [ $code -ne 0 ]; then
            echo "$result"
            GLOBAL_CODE=$code
         fi
      elif [ -n "`echo $n | grep ^N_`" ]; then
         if [ $code -eq 0 ]; then
            echo "$result"
            GLOBAL_CODE=$code
         fi
      else
         echo "$n: no prefix P_ or N_ (positive or negative): skipped"
      fi
   done
}


if [ $# -lt 2 -o $# -gt 3 ]; then
   echo "Usage: $0 <component> <test_directory> <all | broken>"
   echo "    example: $0 parser tests/parser all"
   exit 10
fi

CLASSNAME="TestClass"
COMPONENT="$1.py"
TESTDIR="`echo $2 | sed s~/$~~`"

if [ "$3" = "all" ]; then
   show_all
elif [ "$3" = "broken" ]; then
   show_broken
else
   echo "Third argument must be one of: all broken"
   exit 10
fi

exit $GLOBAL_CODE

